package com.onetoall.yjt;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * Created by DELL on 2017/3/15.
 */

public class ShopingActivity extends Activity {
//beta.yqg.1toall.com.cn/mobile
    private String url = null;
    private WebView webView;
    private JavaScriptObject jsobj;
    private IWXAPI api;

    private WXPayUtils payUtils;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_shop_zhifu);
        url = getIntent().getStringExtra("url");
        Log.e("url",url);
        api = WXAPIFactory.createWXAPI(this, Contants.APP_ID);
        payUtils = new WXPayUtils(ShopingActivity.this);

        webView = (WebView) findViewById(R.id.wv);
        webView.getSettings().setDefaultTextEncodingName("utf-8");
        webView.getSettings().setJavaScriptEnabled(true);
        jsobj = new JavaScriptObject(this);
        webView.getSettings().setLoadWithOverviewMode(true);
        // webSettings.setUseWideViewPort(true);
        // 设置可以支持缩放
        webView.getSettings().setSupportZoom(true);
        // 设置出现缩放工具
        webView.getSettings().setBuiltInZoomControls(true);
        // 扩大比例的缩放
        webView.getSettings().setUseWideViewPort(true);
//		webSettings.setUserAgentString("http://www.zmei.com");
        // 自适应屏幕
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        /**带自动参数的调用事例;
         */
        webView.addJavascriptInterface(jsobj, "myObj");
        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }


        });


    }










    class JavaScriptObject {
        Context mContext;

        public JavaScriptObject(Context zhifuActivity) {
            mContext = zhifuActivity;
        }

        @JavascriptInterface
        public void fun1FromAndroid(String name) {
            Log.e("TAG订单号:",name);
            Toast.makeText(mContext, "正在启动微信支付...", Toast.LENGTH_LONG).show();
            System.out.println("json>>>>>"+name);
            try {
                payUtils.doPay(name);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @JavascriptInterface
        public void fun2(String name) {

        }
    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK)
        {
            if(webView.canGoBack())
            {
                webView.goBack();//返回上一页面
                return true;
            }
            else
            {

                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
